package customer

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/common/response"
	"counter-help/admin/server/model/customer"
	customerReq "counter-help/admin/server/model/customer/request"
	customerRes "counter-help/admin/server/model/customer/response"
	"counter-help/admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type CusOrderAssignmentsApi struct{}

// CreateCusOrderAssignments 创建cusOrderAssignments表
// @Tags CusOrderAssignments
// @Summary 创建cusOrderAssignments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customer.CusOrderAssignments true "创建cusOrderAssignments表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /cusOrderAssignments/createCusOrderAssignments [post]
func (cusOrderAssignmentsApi *CusOrderAssignmentsApi) CreateCusOrderAssignments(c *gin.Context) {
	var cusOrderAssignments customerReq.CreateCusOrderAssignmentsSearch
	err := c.ShouldBindJSON(&cusOrderAssignments)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	isFinal := false
	userId := int(utils.GetUserID(c))
	orderAssignments := &customer.CusOrderAssignments{
		UserId:  &userId,
		OrderId: cusOrderAssignments.OrderId,
		IsFinal: &isFinal,
	}
	err = cusOrderAssignmentsService.CreateCusOrderAssignments(orderAssignments)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteCusOrderAssignments 删除cusOrderAssignments表
// @Tags CusOrderAssignments
// @Summary 删除cusOrderAssignments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customer.CusOrderAssignments true "删除cusOrderAssignments表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /cusOrderAssignments/deleteCusOrderAssignments [delete]
func (cusOrderAssignmentsApi *CusOrderAssignmentsApi) DeleteCusOrderAssignments(c *gin.Context) {
	ID := c.Query("ID")
	err := cusOrderAssignmentsService.DeleteCusOrderAssignments(ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteCusOrderAssignmentsByIds 批量删除cusOrderAssignments表
// @Tags CusOrderAssignments
// @Summary 批量删除cusOrderAssignments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /cusOrderAssignments/deleteCusOrderAssignmentsByIds [delete]
func (cusOrderAssignmentsApi *CusOrderAssignmentsApi) DeleteCusOrderAssignmentsByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	err := cusOrderAssignmentsService.DeleteCusOrderAssignmentsByIds(IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateCusOrderAssignments 更新cusOrderAssignments表
// @Tags CusOrderAssignments
// @Summary 更新cusOrderAssignments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customer.CusOrderAssignments true "更新cusOrderAssignments表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /cusOrderAssignments/updateCusOrderAssignments [put]
func (cusOrderAssignmentsApi *CusOrderAssignmentsApi) UpdateCusOrderAssignments(c *gin.Context) {
	var cusOrderAssignments customer.CusOrderAssignments
	err := c.ShouldBindJSON(&cusOrderAssignments)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = cusOrderAssignmentsService.UpdateCusOrderAssignments(cusOrderAssignments)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindCusOrderAssignments 用id查询cusOrderAssignments表
// @Tags CusOrderAssignments
// @Summary 用id查询cusOrderAssignments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query customer.CusOrderAssignments true "用id查询cusOrderAssignments表"
// @Success 200 {object} response.Response{data=customer.CusOrderAssignments,msg=string} "查询成功"
// @Router /cusOrderAssignments/findCusOrderAssignments [get]
func (cusOrderAssignmentsApi *CusOrderAssignmentsApi) FindCusOrderAssignments(c *gin.Context) {
	ID := c.Query("ID")
	recusOrderAssignments, err := cusOrderAssignmentsService.GetCusOrderAssignments(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(recusOrderAssignments, c)
}

// GetCusOrderAssignmentsList 分页获取cusOrderAssignments表列表
// @Tags CusOrderAssignments
// @Summary 分页获取cusOrderAssignments表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query customerReq.CusOrderAssignmentsSearch true "分页获取cusOrderAssignments表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /cusOrderAssignments/getCusOrderAssignmentsList [get]
func (cusOrderAssignmentsApi *CusOrderAssignmentsApi) GetCusOrderAssignmentsList(c *gin.Context) {
	var pageInfo customerReq.CusOrderAssignmentsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := cusOrderAssignmentsService.GetCusOrderAssignmentsInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetCusOrderAssignmentsPublic 不需要鉴权的cusOrderAssignments表接口
// @Tags CusOrderAssignments
// @Summary 不需要鉴权的cusOrderAssignments表接口
// @accept application/json
// @Produce application/json
// @Param data query customerReq.CusOrderAssignmentsSearch true "分页获取cusOrderAssignments表列表"
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /cusOrderAssignments/getCusOrderAssignmentsPublic [get]
func (cusOrderAssignmentsApi *CusOrderAssignmentsApi) GetCusOrderAssignmentsPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的cusOrderAssignments表接口信息",
	}, "获取成功", c)
}

// GetCusOrdersByUserID 通过userID获取cusOrderAssignments表，并关联获取cusOrder表信息
// @Tags CusOrderAssignments
// @Summary 通过userID获取cusOrderAssignments表，并关联获取cusOrder表信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param userID query int true "用户ID"
// @Success 200 {object} response.Response{data=[]customer.CusOrder,msg=string} "获取成功"
// @Router /cusOrderAssignments/getCusOrdersByUserID [get]
func (cusOrderAssignmentsApi *CusOrderAssignmentsApi) GetCusOrdersByUserID(c *gin.Context) {
	// 获取查询参数 userID
	userID := int(utils.GetUserID(c))

	// 查询 CusOrderAssignments 表
	cusOrderAssignments, err := cusOrderAssignmentsService.GetCusOrderAssignmentsByUserID(userID)
	if err != nil {
		global.GVA_LOG.Error("获取CusOrderAssignments失败!", zap.Error(err))
		response.FailWithMessage("获取CusOrderAssignments失败:"+err.Error(), c)
		return
	}

	// 根据 CusOrderAssignments 中的 order_id 查询 CusOrder 表
	var orders []customerRes.CusOrderSimpleResponse
	for _, assignment := range cusOrderAssignments {
		if assignment.OrderId == nil {
			continue // 跳过没有关联订单的记录
		}
		orderID := strconv.Itoa(*assignment.OrderId) // 将 *int 转换为 string
		order, err := cusOrderService.GetCusOrderByID(orderID)
		if err != nil {
			global.GVA_LOG.Error("获取CusOrder失败!", zap.Error(err))
			response.FailWithMessage("获取CusOrder失败:"+err.Error(), c)
			return
		}
		orders = append(orders, order)
	}

	// 返回结果
	response.OkWithData(orders, c)
}
